package com.example.demospring.business.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demospring.business.entity.Talents;
import com.example.demospring.business.mapper.TalentsMapper;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class TalentsServer extends ServiceImpl<TalentsMapper, Talents> {

    // 查询整张表
    public List<Talents> showTalents(){
        return baseMapper.selectList(null);
    }

    // 查询姓名中含有keyword的人
    public List<Talents> showTalentByNameContaining(String keyword) {
        LambdaQueryWrapper<Talents> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(Talents::getName, keyword);

        return baseMapper.selectList(queryWrapper);
    }
    //修改密码
    public int updatePassword(Integer id) {
        // 查询用户是否存在
        LambdaQueryWrapper<Talents> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Talents::getId, id);
        Talents existingUser = baseMapper.selectOne(queryWrapper);
        LambdaUpdateWrapper<Talents> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Talents::getId, id).set(Talents::getPassword, "111111");
        return baseMapper.update(null, updateWrapper);
    }
    //根据id删除
    public int delTalent(int id){
        return baseMapper.deleteById(id);
    }
    //在表中插入一条数据
    public boolean insertTalent(Talents talent){
        return save(talent);
    }
}
